package dk.pervasivepositioning.project2;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.location.Location;
import android.location.LocationManager;
import android.os.Handler;

public class SmartDistanceBasedStrategy extends ReportingStrategy {

	private final long thresholdMeter;
	private final double maximumspeed;
	private final Timer t = new Timer();
	final Handler handler = new Handler();
	protected boolean isMoving = true;

	public SmartDistanceBasedStrategy(String URL, LocationManager lm,
			int threshold, double maximmumspeed, Activity activity)
			throws IOException {
		super(URL);
		this.maximumspeed = maximmumspeed;
		this.activity = activity;
		this.thresholdMeter = threshold;
		this.lm = lm;
	}

	@Override
	public void startup() {
		startUpdates();
	}

	@Override
	public void stopUpdates() {
		super.stopUpdates();
	}

	@Override
	public void receivedLocation(Location location) throws IOException {
		stopUpdates();
		super.sendData(location);
		if (!isMoving)
			return;
		double temp = (double)thresholdMeter / maximumspeed;
		long delay = (long) (temp * 1000.0);
		t.schedule(new TimerTask() {
			public void run() {
				handler.post(new Runnable() {
					public void run() {
						startUpdates();
					}
				});
			}
		}, delay);
	}

	@Override
	public String getStrategy() {
		return "configurable-maximum-speed";
	}
}
